package com.wifi.channerClass.servlet_;

import com.alibaba.fastjson.JSON;
import com.mysql.jdbc.UpdatableResultSet;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;
import com.wifi.channerClass.entity.ChannelClassEntity;
import com.wifi.channerClass.service.ChannelClassService;
import com.wifi.channerClass.service.ChannelClassServiceImp;
import com.wifi.unti.PageInfo;
import com.wifi.unti.StringUtil;
import com.wifi.user.entity.UnMTotal;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/channelClass/*")
public class ChannelClassSevlet_  extends BaseServlet {
          ChannelClassService service=  new ChannelClassServiceImp();
    @ShenZhouWifiMethod("/find")
    public  void find(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

       request.getRequestDispatcher("/WEB-INF/channelClass/channelClassList.jsp").forward(request,response);

    }
    @ShenZhouWifiMethod("/doFind")
    public  void doFind(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//        request.getRequestDispatcher("/WEB-INF/channelClass/channelClassList.jsp").forward(request,response);
        String pageNum=request.getParameter("pageNum");
        String pageSize=request.getParameter("pageSize");
        String pid=request.getParameter("fatherid");
        long totalNum = service.selectCount(pid);

        PageInfo pageInfo = new PageInfo(pageNum,pageSize,(int)totalNum );
           new PageInfo(pageNum,pageSize,(int)totalNum);

        List<Object> menuPage=service.getDatePage(pageInfo.getStart(),pageInfo.getPageSize(),pid);
        pageInfo.setList(menuPage);
        response.getWriter().write(JSON.toJSONString(pageInfo));
    }

    @ShenZhouWifiMethod("/findFather")
    public  void findFather(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("fird");
        List<Object> menufather=service.getfather();
        response.getWriter().write(JSON.toJSONString(menufather));
    }

    @ShenZhouWifiMethod("/add")
    public void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String pid = request.getParameter("fatherSelect");
        System.out.println(name);
        System.out.println(pid);
        if (name.isEmpty() || pid.isEmpty()) {
            response.getWriter().write("2");
        }else {
            //添加 渠道分类；
           long i= service.insert(name,Integer.valueOf(pid));
           if (i>0){
               response.getWriter().write("1");
           }else{
               response.getWriter().write("3");
           }


        }
    }


    @ShenZhouWifiMethod("/remove")
    public  void  remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        Object o = service.queryByid(id);
        if (o == null) {
            response.getWriter().write("2");
        } else {
            Integer status = ((ChannelClassEntity) o).getStatus();
            if (status == 0) {
                status = 1;
            } else {
                status = 0;
            }
            long remove = service.remove(Integer.valueOf(id), status);
            if (remove > 0) {
                response.getWriter().write("1");
            } else {
                response.getWriter().write("3");
            }
        }
    }
             @ShenZhouWifiMethod("/editget")
        public  void  editget(HttpServletRequest request, HttpServletResponse response) throws IOException {
                 String id = request.getParameter("id");

                 System.out.println(id);
                 Object o = service.queryByid(id);
                 if (o == null) {
                     response.getWriter().write("2");
                 } else {
                   response.getWriter().write(JSON.toJSONString(o));

                 }


    }
    @ShenZhouWifiMethod("/edit")
    public  void  edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id=request.getParameter("ide");
        String name=request.getParameter("namee");
        String role=request.getParameter("rolee");
        String active=request.getParameter("activee");

   int i= Integer.valueOf(id);   int l= Integer.valueOf(role);

        if(i==l){response.getWriter().write("3");}
        else {
        ChannelClassEntity channelClassEntity=new ChannelClassEntity(Integer.valueOf(id),name,Integer.valueOf(role),null);
        int n=service.edit(channelClassEntity);
        if (n>0){response.getWriter().write("1");}else {response.getWriter().write("2");}}
    }
}
